package com.bw.mvvm.mvvm.view;

import android.content.Intent;
import android.os.Bundle;


import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvvm.fleld.UIField;
import com.bw.mvvm.mvvm.viewmodel.BaseViewModel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

public abstract class BaseActivity<V extends ViewDataBinding,VM extends BaseViewModel>
    extends AppCompatActivity implements IView {

    protected V mView;

    protected VM mViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewModel();

        getLifecycle().addObserver(mViewModel);
        mViewModel.getUiChange().getShowMsgObservable().observe(this,showMsgObserver);
        mViewModel.getUiChange().getShowDialogObservable().observe(this,showDialogObserver);
        mViewModel.getUiChange().getHideDialogObservable().observe(this,hideDialogObserver);
        mViewModel.getUiChange().getFinishActObservable().observe(this,finishPageObserver);
        mViewModel.getUiChange().getStartPageObservable().observe(this,startPageObserver);
        bindDataBinding();
        mView.setVariable(initVerable(),mViewModel);
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }
    private Observer<String> showMsgObserver = new Observer<String>() {
       @Override
       public void onChanged(String s) {
           showMsg(s);
       }
    };

    private Observer<String> showDialogObserver = new Observer<String>() {
        @Override
        public void onChanged(String s) {
            showDialog();
        }
    };

    private Observer<String> hideDialogObserver = new Observer<String>() {
        @Override
        public void onChanged(String s) {
            hideDialog();
        }
    };

    private Observer<String> finishPageObserver = new Observer<String>() {
        @Override
        public void onChanged(String s) {
            finishPage();
        }
    };
    private Observer<HashMap<String,Object>> startPageObserver = new Observer<HashMap<String, Object>>() {
        @Override
        public void onChanged(HashMap<String, Object> map) {
            Class cs = (Class) map.get(UIField.PAGECS);
            Object o = map.get(UIField.PAGEBUNDLE);
            Bundle bundle = null;
            if (o != null){
                bundle = (Bundle) o;
                startPage(cs,bundle);
            }
            startPage(cs,null);
        }
    };

    @Override
    public void showMsg(String msg) {
        ToastUtils.showShort(msg);
    }

    @Override
    public void finishPage() {
        finish();
    }

    @Override
    public void startPage(Class cs, Bundle bundle) {
        Intent intent = new Intent(this,cs);
        if (bundle != null){
            intent.putExtra(UIField.PAGEBUNDLE,bundle);
        }
        startActivity(intent);
    }

    @Override
    public void initViewModel() {
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        Type[]types = parameterizedType.getActualTypeArguments();
        Class clazz = (Class) types[1];
        mViewModel = (VM)new ViewModelProvider(this,
                new ViewModelProvider.NewInstanceFactory()).get(clazz);
    }

    @Override
    public void bindDataBinding() {
        mView = DataBindingUtil.setContentView(this,bindLayout());
    }

}
